<?php

class model_moduleposition extends database_mysql {

    public function md_moduleposition_get_list_com() {
        $strSql = "";
        $tb_com = prefix . 'categories ';
        $tb_com_lang = prefix . "categories_lang";

        $strSql = "SELECT 
                        com.ID, 
                        col.CatName 
                    FROM 
                        $tb_com AS com ,
                        $tb_com_lang AS col 
                    WHERE
                        com.ID = col.CatID 
                        AND col.`Status` = true 
                        AND com.Define = 'com' 
                        
                        AND col.LangID = " . LANG;
        $row = parent::mysql_fetch_all_rows($strSql);
        return $row;
    }

    public function md_moduleposition_get_list_position_by_com($id_com) {
        $strSql = "";
        $tb_pos = prefix . 'modules_position ';
        $tb_module = prefix . "modules";
        $tb_module_lang = prefix . "modules_lang";

        $strSql = "SELECT
                        mol.ModuleTitle,
                        modu.ID,
                        pos.Position,
                        pos.Order,
                        pos.`Status`,
                        pos.`ComID`
                    FROM
                        $tb_pos AS pos
                    INNER JOIN $tb_module AS modu ON 
                        pos.ModuleID = modu.ID  AND  modu.AppID =  1" .
                " INNER JOIN $tb_module_lang AS mol ON 
                        modu.ID = mol.ModulesID AND 
                        mol.LangID = " . LANG
                . " AND mol.Status = 'true' AND " .
                "pos.ComID = " . $id_com
                . " ORDER BY pos.Order";

        $row = parent::mysql_fetch_all_rows($strSql);
        return $row;
    }

    public function md_moduleposition_get_list_module() {
        $strSql = "";
        $strSql = "";
        $tb_module = '';
        $tb_module_lang = '';

        $tb_module = prefix . "modules";
        $tb_module_lang = prefix . "modules_lang";
        $strSql = "SELECT
                        mol.ModuleTitle,
                        modu.ID
                    FROM
                        $tb_module_lang  mol,
                        $tb_module  modu 
                    WHERE
                        mol.ModulesID = modu.ID
                        AND modu.AppID =1                        
                        AND mol.LangID = " . LANG;

        $row = parent::mysql_fetch_all_rows($strSql);
        return $row;
    }

    public function md_moduleposition_get_list_module_public($position, $Com_id) {
        $strSql = "";
        $strSql = "";
        $tb_module = '';
        $tb_module_lang = '';

        $tb_module = prefix . "modules";
        $tb_module_lang = prefix . "modules_lang";
        $modules_position = prefix . "modules_position";

        $strSql = "SELECT
                        mol.ModuleTitle,
                        modu.ID
                    FROM
                        $tb_module_lang  mol,
                        $tb_module  modu 
                    WHERE
                        mol.ModulesID = modu.ID
                        AND modu.AppID =1
                        AND modu.ID NOT IN 
                            (SELECT po2.ModuleID 
                             FROM 
                                $modules_position po2 
                             WHERE 
                                po2.Position = '$position' AND po2.ComID = $Com_id )
                        AND mol.LangID = " . LANG;


        $row = parent::mysql_fetch_all_rows($strSql);
        return $row;
    }

    public function md_moduleposition_add_module_position($module, $com, $position, $order, $status) {


        $sql = "INSERT INTO `" . prefix . "modules_position` (`Position`,`ModuleID`, `ComID`,`Order`, `Status`) 
                VALUES ('" . $position . "','" . $module . "', '" . $com . "', '" . $order . "','" . $status . "')";

        $rsmod = $this->mysql_update_insert($sql);

        if ($rsmod) {
            $rs = true;
        } else {
            $rs = false;
        }
        return $rs;
    }

    public function md_moduleposition_update_module_position($module, $com, $position, $status) {
        $tb_modules = '';
        $rs = '';
        $tb_modules = prefix . "modules_position";
        $sql = '';
        $sql = "UPDATE `$tb_modules` 
                        SET 
                            `Position`='$position',
                            `ModuleID`='$module', 
                            `ComID`='$com' ,                            
                            `Status`='$status ' 
                        WHERE 
                            `Position`in ('$position')
                            and `ModuleID`in ('$module')
                            and `ComID`in ('$com')";
        $rs = parent::mysql_update_insert($sql);

        return $rs;
    }

    public function md_moduleposition_update_order_module_position($module, $com, $position, $order) {

        $rs = '';
        $tb_position = prefix . "modules_position";
        $sql = '';
        $sql = "UPDATE `$tb_position` 
                        SET 
                            `Order`='$order' 
                        WHERE 
                            `Position`in ('$position')
                            and `ModuleID`in ('$module')
                            and `ComID`in ('$com')";

        $rs = parent::mysql_update_insert($sql);

        return $rs;
    }

    public function md_moduleposition_delete_module_position($module, $com, $position) {
        $tb_modules = '';

        $rs = false;
        $tb_modules = prefix . "modules_position";
        $sql = "DELETE FROM 
                    `$tb_modules`
                WHERE 
                    `Position`in ('$position')
                and `ModuleID`in ('$module')
                and `ComID`in ('$com')";
        $rs = parent::mysql_delete($sql);
        return $rs;
    }

    public function md_moduleposition_getPosition($Position, $ModID, $ComID) {
        $strSQL = "SELECT
                        pos.Position,
                        pos.ModuleID,
                        
                        pos.Order,
                        pos.`Status`
                    FROM
                        " . prefix . "modules_position AS pos
                    WHERE
                        pos.Position = '$Position' AND
                        pos.ModuleID = $ModID AND
                        pos.ComID = $ComID";
        $row = parent::mysql_fetch_all_rows($strSQL);
        return $row;
    }

    public function md_moduleposition_addPosCopy($rowPosNguon, $ComID) {
        $sql = "INSERT INTO `" . prefix . "modules_position` (`Position`,`ModuleID`, `ComID`,`Order`, `Status`) 
                VALUES ('" . $rowPosNguon['Position'] . "','" . $rowPosNguon['ModuleID'] . "', '" . $ComID . "', '" . $rowPosNguon['Order'] . "','" . $rowPosNguon['Status'] . "')";
        $rsmod = $this->mysql_update_insert($sql);
        return $rsmod;
    }

    public function md_moduleposition_CopyPos($arrData, $ComID) {
        //  $value['Position'] . '-' . $value['ID'] . '-' . $value['ComID'];
        $arrValue = explode('-', $arrData);
        $rowPosNguon = $this->md_moduleposition_getPosition($arrValue[0], $arrValue[1], $arrValue[2]);
        var_dump($rowPosNguon);
        if(!empty($rowPosNguon))
        {
           $this->md_moduleposition_addPosCopy($rowPosNguon[0], $ComID);
        }
    }

}

